package com.bootdo.company.service.impl;

import com.bootdo.company.dao.CompanyDao;
import com.bootdo.company.dao.CompanyPublishPostDao;
import com.bootdo.company.domain.CompanyDO;
import com.bootdo.company.domain.CompanyPublishPostDO;
import com.bootdo.system.domain.IndustryDO;
import com.bootdo.system.domain.JobIndustry;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.IndustryService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.bootdo.company.dao.PostDao;
import com.bootdo.company.domain.PostDO;
import com.bootdo.company.service.PostService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class PostServiceImpl implements PostService {
	@Autowired
	private PostDao postDao;
	@Autowired
	IndustryService industryService;
	@Autowired
	CompanyPublishPostDao companyPublishPostDao;
	@Autowired
	private CompanyDao companyDao;
	
	@Override
	public PostDO get(Long id){
		return postDao.get(id);
	}
	
	@Override
	public List<PostDO> list(Map<String, Object> map){
		return postDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return postDao.count(map);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public int save(PostDO post) throws Exception {
		post.setReleaseTime(new Date());
		postDao.save(post);

		CompanyPublishPostDO companyPublishPost = new CompanyPublishPostDO();

		Subject subject = SecurityUtils.getSubject();
		UserDO user = (UserDO)subject.getPrincipal();
		if(user==null){
			throw new Exception("未登录");
		}

		CompanyDO company = companyDao.getByUserId(user.getUserId());
		if(company == null  ){
			throw new Exception("请先建立企业基本信息");
		}

		companyPublishPost.setCompanyid(company.getId());
		//companyPublishPost.setJobPosition(post.getJobPosition());
		companyPublishPost.setPostid(post.getId());
		companyPublishPostDao.save(companyPublishPost);

		return 1;
	}
	
	@Override
	public int update(PostDO post){
		return postDao.update(post);
	}
	
	@Override
	public int remove(Long id){
		return postDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return postDao.batchRemove(ids);
	}



	public List<Map<String, Object>> statPostByIndustry() {
		List<Map<String, Object>> statMap = postDao.statPostByIndustry();
		List<IndustryDO> industries = industryService.list(new HashMap<>());
		Map<Integer, List<IndustryDO>> industryTree = new LinkedHashMap<>();
		Map<Integer, IndustryDO> nameMap = new HashMap<>();
		for (IndustryDO jobIndustry : industries) {
			if (jobIndustry.getParentNo() == 0) {
				nameMap.put(jobIndustry.getIndustryNo(), jobIndustry);
				continue;
			}
			for (Map map : statMap) {
				if(map.get("industry_no")==null){
					continue;
				}
				if ((Long) map.get("industry_no") == jobIndustry.getIndustryNo().longValue()) {
					jobIndustry.setStatNum(((Long) map.get("num")).intValue());
				}
			}
			List<IndustryDO> list = industryTree.computeIfAbsent(jobIndustry.getParentNo(), k -> new LinkedList<>());
			list.add(jobIndustry);
		}

		List<Map<String, Object>> returnMap = new ArrayList<>();
		for (Map.Entry<Integer, List<IndustryDO>> entry : industryTree.entrySet()) {
			Map<String, Object> map = new HashMap<>();
			map.put("industryName", (nameMap.get(entry.getKey()).getIndustryName()));
			map.put("id", (nameMap.get(entry.getKey()).getId()));
			map.put("child", entry.getValue());
			returnMap.add(map);
		}
		return returnMap;
	}

	public List<Map<String, Object>> search(Map<String, Object> map){
		return postDao.search(map);
	}



	@Override
	public JobIndustry getJobIndustry(Long industryNo) {
		return  postDao.getJobIndustry(industryNo);
	}

	@Override
	public int count() {
		return postDao.count(new HashMap<>());
	}
	
}
